package com.ztom.top100;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * 子集
 * <p>
 * https://leetcode-cn.com/problems/subsets/
 *
 * @author ZhangTao
 */
public class Code33Subsets {

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return res;
        }

        process(nums, 0, res, new LinkedList<>());
        return res;
    }

    private void process(int[] nums, int index, List<List<Integer>> res, Deque<Integer> collect) {
        if (index == nums.length) {
            // 所有位置都处理完, 收集答案
            res.add(new ArrayList<>(collect));
            return;
        }

        // 要当前位置, 需要回溯
        collect.addLast(nums[index]);
        process(nums, index + 1, res, collect);
        collect.pollLast();

        // 不要当前位置
        process(nums, index + 1, res, collect);
    }
}
